package com.easy.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.OrderAddr;
import com.easy.common.service.IOrderAddrService;
import com.easy.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order/addr")
@CrossOrigin
public class OrderAddrController {

    @Autowired
    private IOrderAddrService orderAddrService;

    // 根据id获取订单地址信息
    @GetMapping("/id/{id}")
    public Result getOrderAddrById(@PathVariable Integer id) {
        return Result.success(orderAddrService.getOrderAddrById(id));
    }

    // 根据用户id获取订单地址列表
    @GetMapping("/user/{userId}")
    public Result getOrderAddrsByUserId(@PathVariable Integer userId) {
        List<OrderAddr> orderAddrs = orderAddrService.getOrderAddrsByUserId(userId);
        return Result.success(orderAddrs);
    }

    // 根据秒杀商品id获取订单地址列表
    @GetMapping("/seckillgoods/{seckillGoodsId}")
    public Result getOrderAddrsBySeckillGoodsId(@PathVariable Integer seckillGoodsId) {
        List<OrderAddr> orderAddrs = orderAddrService.getOrderAddrsBySeckillGoodsId(seckillGoodsId);
        return Result.success(orderAddrs);
    }

    // 根据商品id获取订单地址列表
    @GetMapping("/goods/{goodsId}")
    public Result getOrderAddrsByGoodsId(@PathVariable Integer goodsId) {
        List<OrderAddr> orderAddrs = orderAddrService.getOrderAddrsByGoodsId(goodsId);
        return Result.success(orderAddrs);
    }

    // 添加订单地址
    @PostMapping("/add")
    public Result addOrderAddr(@RequestBody OrderAddr orderAddr) {
        OrderAddr result = orderAddrService.addOrderAddr(orderAddr);
        return result != null ? Result.success(200, "添加成功",orderAddr) : Result.fail(500, "添加失败");
    }

    // 更新订单地址
    @PutMapping("/update")
    public Result updateOrderAddr(@RequestBody OrderAddr orderAddr) {
        int result = orderAddrService.updateOrderAddr(orderAddr);
        return result > 0 ? Result.success("更新成功") : Result.fail(500, "更新失败");
    }

    // 删除订单地址（软删除）
    @DeleteMapping("/delete/{id}")
    public Result deleteOrderAddr(@PathVariable Integer id) {
        int result = orderAddrService.deleteOrderAddr(id);
        return result > 0 ? Result.success("删除成功") : Result.fail(500, "删除失败");
    }

    // 获取订单地址列表
    @GetMapping("/list")
    public Result getOrderAddrList(OrderAddr orderAddr) {
        return Result.success(orderAddrService.getAll(orderAddr));
    }

    // 获取订单地址分页列表
    @GetMapping("/page")
    public Result getOrderAddrPage(OrderAddr orderAddr, Integer current, Integer size) {
        IPage<OrderAddr> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size);
        return Result.success(orderAddrService.getPage(orderAddr, page));
    }
}
